VERSION = 1
PATCHLEVEL = 0
SUBLEVEL = 0
EXTRAVERSION =
NAME = Device Service

MAKEFLAGS += -rR --include-dir=$(CURDIR)
MAKEFLAGS += --no-print-directory

ifeq ("$(origin V)", "command line")
  KBUILD_VERBOSE = $(V)
endif
ifndef KBUILD_VERBOSE
  KBUILD_VERBOSE = 0
endif

ifeq ($(KBUILD_VERBOSE),1)
  quiet =
  Q =
else
  quiet=quiet_
  Q = @
endif

# If the user is running make -s (silent mode), suppress echoing of
# commands

ifneq ($(filter 4.%,$(MAKE_VERSION)),)	# make-4
ifneq ($(filter %s ,$(firstword x$(MAKEFLAGS))),)
  quiet=silent_
  tools_silent=s
endif
else					# make-3.8x
ifneq ($(filter s% -s%,$(MAKEFLAGS)),)
  quiet=silent_
  tools_silent=-s
endif
endif
export quiet Q KBUILD_VERBOSE

version_h := include/generated/uapi/linux/version.h

HOSTCC       = gcc
HOSTCXX      = g++
HOSTCFLAGS   = -Wall -Wmissing-prototypes -Wstrict-prototypes -O2 -fomit-frame-pointer -std=gnu99
HOSTCXXFLAGS = -O2

# Call a source code checker (by default, "sparse") as part of the
# C compilation.
#
# Use 'make C=1' to enable checking of only re-compiled files.
# Use 'make C=2' to enable checking of *all* source files, regardless
# of whether they are re-compiled or not.
#
# See the file "Documentation/dev-tools/sparse.rst" for more details,
# including where to get the "sparse" utility.

ifeq ("$(origin C)", "command line")
  KBUILD_CHECKSRC = $(C)
endif
ifndef KBUILD_CHECKSRC
  KBUILD_CHECKSRC = 0
endif
export KBUILD_CHECKSRC

CROSS_COMPILE ?= arm-linux-

AS     = $(CROSS_COMPILE)as
LD     = $(CROSS_COMPILE)ld
CC     = $(CROSS_COMPILE)gcc
CXX    = $(CROSS_COMPILE)g++
AR     = $(CROSS_COMPILE)ar
STRIP  = $(CROSS_COMPILE)strip
OBJCOPY= $(CROSS_COMPILE)objcopy
OBJDUMP= $(CROSS_COMPILE)objdump
UPXR   = upx

PHONY := _all
_all:

srctree  := $(CURDIR)
LIB_PATH :=
EXT_LIB  :=

MACH ?= NUC970

CFLAGS := -g -Wall -O2 -D$(MACH)

export AS LD CC CXX AR STRIP OBJCOPY OBJDUMP HOSTCC HOSTCXX HOSTCFLAGS HOSTCXXFLAGS
export srctree LIB_PATH EXT_LIB CFLAGS UPXR

# We need some generic definitions (do not try to remake the file).
$(srctree)/scripts/Kbuild.include: ;
include $(srctree)/scripts/Kbuild.include

# Read KERNELRELEASE from include/config/kernel.release (if it exists)
KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(SUBLEVEL)))$(EXTRAVERSION)
export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION

filechk_kernel.release = echo "$(KERNELVERSION)"

# Store (new) KERNELRELEASE string in include/config/kernel.release
include/config/kernel.release: FORCE
	$(call filechk,kernel.release)

# ===========================================================================
# Rules shared between *config targets and build targets

# Basic helpers built in scripts/
PHONY += scripts_basic archprepare
scripts_basic:
	$(Q)$(MAKE) $(build)=scripts/basic
	$(Q)rm -f .tmp_quiet_recordmcount

# To avoid any implicit rule to kick in, define an empty command.
scripts/basic/%: scripts_basic ;

archprepare: include/config/kernel.release $(version_h) include/generated/utsrelease.h
	@:

prepare: archprepare scripts_basic

libs-y  := 
app-y   := service/ test/

TARGET:= $(srctree)/bin/apps.tgz

PHONY += all
_all: all

all: prepare $(TARGET)
	@now=`date '+%F %T'`; echo "all done at $${now}"

all-dirs   := $(patsubst %/, %, $(filter %/, $(libs-y) $(app-y)))

libs-y1	  := $(patsubst %/, %/built-in.o, $(libs-y))
libs-y2	  := $(patsubst %/, %/lib.a, $(libs-y))
libs-y	  := $(libs-y1) $(libs-y2)

$(sort $(libs-y) $(app-y)): $(all-dirs) ;

$(TARGET): $(libs-y) $(app-y) 
	@
#	$(Q)$(kecho) 'Target[$@] is ready'

PHONY += $(all-dirs)
$(all-dirs):
	$(Q)$(MAKE) $(build)=$@

# Generate some files
# ---------------------------------------------------------------------------

# KERNELRELEASE can change from a few different places, meaning version.h
# needs to be updated, so this check is forced on all builds

uts_len := 64
define filechk_utsrelease.h
	if [ `echo -n "$(KERNELRELEASE)" | wc -c ` -gt $(uts_len) ]; then \
	  echo '"$(KERNELRELEASE)" exceeds $(uts_len) characters' >&2;    \
	  exit 1;                                                         \
	fi;                                                               \
	echo \#define UTS_RELEASE \"$(KERNELRELEASE)\"
endef

define filechk_version.h
	echo \#define LINUX_VERSION_CODE $(shell                         \
	expr $(VERSION) \* 65536 + 0$(PATCHLEVEL) \* 256 + 0$(SUBLEVEL)); \
	echo '#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))'
endef

$(version_h):
	$(call filechk,version.h)

include/generated/utsrelease.h: include/config/kernel.release FORCE
	$(call filechk,utsrelease.h)


clean-dirs  := $(addprefix _clean_, . $(all-dirs))
PHONY += $(clean-dirs) clean
$(clean-dirs):
	$(Q)$(MAKE) $(clean)=$(patsubst _clean_%,%,$@)

clean: $(clean-dirs)
	@rm -f $(TARGET)
	@find . \
		\( -name '*.[os]' -o -name '.*.cmd' \
		-o -name '.*.d' -o -name '*.mod.c' \
		\) -type f -print | xargs rm -f

PHONY += FORCE
FORCE:

.PHONY: $(PHONY)
